﻿using System.Globalization;
using System.Web.Compilation;
using System.Web.Mvc;

namespace DiplomaWeb.Core
{
    public static class LocalizationExtentions
    {
        public static string Resource(this HtmlHelper html, string expr, params object[] args)
        {
            string path = ((WebFormView) html.ViewContext.View).ViewPath;

            var fields = (ResourceExpressionFields) (new ResourceExpressionBuilder()).ParseExpression(
                expr,
                typeof (string),
                new ExpressionBuilderContext(path)
                                                        );

            return (!string.IsNullOrEmpty(fields.ClassKey))
                       ? string.Format((string) html.ViewContext.HttpContext.GetGlobalResourceObject(
                           fields.ClassKey,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture),
                                       args)
                       : string.Format((string) html.ViewContext.HttpContext.GetLocalResourceObject(
                           path,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture),
                                       args);
        }
    }
}